﻿using System;

namespace ProblemsSet
{
    public class Problem_138 : BaseProblem
    {
        public override object GetResult()
        {
            const int max = 12;

            long cnt = 0;
            long res = 0;

            for (long a = 2; a <= long.MaxValue; a++)
            {
                var bb = Math.Sqrt(5*a*a + 1) - 2*a;
                if (bb > 0 && bb < a && Math.Truncate(bb) == bb)
                {
                    cnt++;
                    res += (long)(a*a + bb*bb);
                    if (cnt == max)
                        return res;
                }
                bb = Math.Sqrt(5 * a * a - 1) - 2 * a;
                if (bb <= 0 || bb >= a || Math.Truncate(bb) != bb) continue;
                cnt++;
                res += (long)(a * a + bb * bb);
                if (cnt == max)
                    return res;
            }
            return -1;
        }

        public override string Problem
        {
            get
            {
                return @"Consider the isosceles triangle with base length, b = 16, and legs, L = 17.


By using the Pythagorean theorem it can be seen that the height of the triangle, h = (172  82) = 15, which is one less than the base length.

With b = 272 and L = 305, we get h = 273, which is one more than the base length, and this is the second smallest isosceles triangle with the property that h = b  1.

Find  L for the twelve smallest isosceles triangles for which h = b  1 and b, L are positive integers.";
            }
        }

        public override bool IsSolved
        {
            get
            {
                return true;
            }
        }

        public override object Answer
        {
            get
            {
                return 1118049290473932;
            }
        }
    }
}
